//
//  YYMineVC.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/6.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYMineVC.h"
#import "YYMineCell.h"
#import "YYGradeVC.h"
#import "YYMessageVC.h"
#import "YYMyOrderVC.h"
#import "YYSetVC.h"
#import "YYQuestionVC.h"
#import "YYInspectVC.h"
#import "YYMemberVC.h"
#import "YYInviteVC.h"
#import "NEWMyOrderVC.h"

@interface YYMineVC ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray * iconArray;
@property(nonatomic,strong)NSArray * titleArray;
@property(nonatomic,copy)NSString * count;
@property(nonatomic,copy)NSString * source;
@property(nonatomic,copy)NSString * phone;

@end

@implementation YYMineVC


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    if (@available(iOS 11.0, *)) {//解决隐藏导航栏scrollView不能到顶的问题
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}


- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.bgView.image = [CFUtility gradientImageWithBounds:self.bgView.bounds andColors:@[DEF_COLOR_MAIN,DEF_COLOR_MAIN1] andGradientType:1];
    self.sourceView.layer.shadowOffset = CGSizeMake(1, 1);
    self.sourceView.layer.shadowOpacity = 0.8;
    self.sourceView.layer.shadowColor = [UIColor colorWithWhite:0.7 alpha:1].CGColor;
    [self getData];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.viewTop.constant = -DEF_NAV_MORE_HEIGHT()-20;
}
- (void)getData{
    [RequestOperationManager personalIndex_success:^(id result) {
        [UserCenterData sharedInstance].userInfo = result[@"data"][@"list"];
        self.count = result[@"count"];
        int grade = [result[@"data"][@"list"][@"grade"] intValue];
        self.source = result[@"data"][@"list"][@"integral"];
        self.lbGrade.text =[NSString stringWithFormat:@"V%i",grade];
        //sign_member_v1
        self.leaveWidth.constant = 130.0*(grade/4.0);
        [UIView animateWithDuration:0.6 animations:^{
            [self.view layoutIfNeeded];
        }];
        self.lbLeave.text = [NSString stringWithFormat:@"V%i",grade];
        self.lbPhone.text = result[@"data"][@"list"][@"iphone"];
        self.lbSource.text = self.source;
    } failture:^(NSError *error) {
        
    }];
    [RequestOperationManager personalAccident_success:^(id result) {
        if (DEF_REQ_SUCCESS) {
            self.phone = result[@"data"][@"iphone"];
        }
    } failture:^(NSError *error) {
        
    }];
}


- (IBAction)toUserCenter:(id)sender {
    YYGradeVC * grade = (YYGradeVC *)[CFUtility getVC:@"YYGradeVC" Storyboard:MINE];
    [self.navigationController pushViewController:grade animated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 5;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 16;
    }
    return 0;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YYMineCell * cell = [tableView dequeueReusableCellWithIdentifier:@"YYMineCell"];
    if (indexPath.section == 0) {
        cell.icon.image = DEF_IMAGENAME(@"jiushengzhongxin");
        cell.lbTitle.text = @"九盛会员中心";
        cell.lbContent.hidden = YES;
        cell.line.hidden = YES;
    }else if (indexPath.section == 1){
        cell.icon.image = DEF_IMAGENAME(self.iconArray[indexPath.row]);
        cell.lbTitle.text = self.titleArray[indexPath.row];
        if (indexPath.row == 3) {
            if ([self.count intValue] == 0) {
                 cell.lbContent.hidden = YES;
            }else{
                cell.lbContent.text = [NSString stringWithFormat:@"您有%@条消息",self.count];
                cell.lbContent.hidden = NO;
            }
        }else{
            cell.lbContent.hidden = YES;
        }
        if (indexPath.row == 4) {
            cell.line.hidden = YES;
        }else{
            cell.line.hidden = NO;
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        YYMemberVC * member = (YYMemberVC *)[CFUtility getVC:@"YYMemberVC" Storyboard:MINE];
        [self.navigationController pushViewController:member animated:YES];
        
    }else if (indexPath.section == 1) {
//        if (indexPath.row == 0) {
//            NSString *callPhone = [NSString stringWithFormat:@"telprompt://%@",self.phone];
//            if (@available(iOS 10.0, *)) {
//                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone] options:@{} completionHandler:nil];
//            } else {
//                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:callPhone]];
//            }
//        }
        if (indexPath.row == 0) {
            NEWMyOrderVC * myOrder = (NEWMyOrderVC *)[CFUtility getVC:@"NEWMyOrderVC" Storyboard:MINE];;
            [self.navigationController pushViewController:myOrder animated:YES];
        }
        if (indexPath.row == 1) {
            YYQuestionVC * question = (YYQuestionVC *)[CFUtility getVC:@"YYQuestionVC" Storyboard:MINE];
            [self.navigationController pushViewController:question animated:YES];
        }
        if (indexPath.row == 2) {
            YYSetVC * set = (YYSetVC *)[CFUtility getVC:@"YYSetVC" Storyboard:MINE];
            [self.navigationController pushViewController:set animated:YES];
        }
        if (indexPath.row == 3) {
            YYMessageVC * message = (YYMessageVC *)[CFUtility getVC:@"YYMessageVC" Storyboard:MINE];
            [self.navigationController pushViewController:message animated:YES];
        }
        if (indexPath.row == 4) {
            YYInviteVC * invite = (YYInviteVC *)[CFUtility getVC:@"YYInviteVC" Storyboard:MINE];
            [self.navigationController pushViewController:invite animated:YES];
        }
    }
}

-(NSArray *)iconArray{
    if (!_iconArray) {
        _iconArray = @[@"mine_dingdan",@"mine_kefu",@"mine_shezhi",@"mine_xiaoxi",@"mine_yaoqing"];
    }
    return _iconArray;
}

-(NSArray *)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"我的订单",@"客服",@"设置",@"消息",@"邀请好友"];
    }
    return _titleArray;
}
@end
